package com.yanqu.road.dao.impl.config;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.config.promotion.PromotionInfo;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class PromotionInfoDaoImpl extends TempDao {
    private static String getPromotionInfoSql = "select * from `t_p_promotion` order by `characterId`";

    public Map<Integer, List<PromotionInfo>> getPromotionInfoList() {
        Map<Integer, List<PromotionInfo>> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall(getPromotionInfoSql);
            rs = call.executeQuery();
            while (rs.next()) {
                PromotionInfo promotionInfo = new PromotionInfo();
                promotionInfo.setId(rs.getInt("id"));
                promotionInfo.setType(rs.getInt("type"));
                promotionInfo.setParamList(rs.getString("paramList"));
                promotionInfo.setNextId(rs.getInt("nextId"));
                promotionInfo.setCharacterId(rs.getInt("characterId"));
                promotionInfo.setQuality(rs.getInt("quality"));
                promotionInfo.setDefaultAttribute(rs.getString("defaultAttribute"));
                promotionInfo.setSkillList(StringUtils.stringToIntegerList(rs.getString("skills"), "\\|"));
                promotionInfo.setTalentList(StringUtils.stringToIntegerList(rs.getString("talents"), "\\|"));
                promotionInfo.setCanStage(rs.getInt("canStage"));
                promotionInfo.setReward(rs.getString("reward"));
                if (map.keySet().contains(rs.getInt("characterId"))) {
                    map.get(rs.getInt("characterId")).add(promotionInfo);
                } else {
                    List<PromotionInfo> list = new ArrayList<>();
                    list.add(promotionInfo);
                    map.put(rs.getInt("characterId"), list);
                }
                promotionInfo.setExtendParam(rs.getString("extendParam"));
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

}
